Variables
Variables allow a programmer to store a value so it can be used later. This is important because it means that data does not need to be used as soon as it has been calculated. Also, any data that has been stored in a variable can be used multiple times. This saves a computer time because it does not have to do any calculations required to get the data over and over again. Also, it makes code more readable because the programmer is able to reference a variable by name instead of retyping the calculation. Assignment Variables are storage locations that hold values so they may be used later. This is done through assignment, which you have seen earlier like this: hello = "Hello World" This assigns the string of charcters "Hello World" to a variable "hello". This variable was then used later: print hello Other data, such as numbers, can be stored in a variable: sum = 5 and calculations are often done to store a result into the variable: sum = 6 + 10 # sum is now 16 Variables can be reassigned. This means that the value in the variable can be changed: Types In many programming languages, including Python, variables have certain types that determine what sort of operations can be performed on them and/or the affect the operations have on those variables. Five common data types in programming are: #Integers #*1, 5, 405, -6 #Booleans #*True, False #Characters #*'a', 'y', '5', '$' #Floating-point numbers #*2.4, -50.67, 19000.11 #Strings #*"Hello", "A string of alphanumeric ch4r4ct3rs.", "!#^$?" Python uses all of the above except for characters, which it instead just represents as strings of length 1. For numbers, along with integers and floating-point numbers, Python has a complex number data type. The complex number data type is used for representing mathematical complex numbers, used in scientific fields and engineering. Python's data structures, which we will talk about in a later section, are also considered Pythonic data types. Although variables are typed in Python, they are not statically typed. This means that a variable's type does not have to be declared before it is assigned. For example, in Python you may do this: a = 5 a = "This is a string!" However, in a statically typed language such as Java: int a = 5 // You must first declare the variable a = "I am not a string!" // This throws an error! As mentioned earlier, types determine what sort of operations are allowed on your variables. This helps the interpreter run your code more efficiently by learning the type of a variable when you first assignment. Let's take a look: my_var = "A String" # reverse is a string function that reverses the characters in the string # The interpreter knows that my_var is a string here # so it allows reverse to be called upon it my_var.reverse my_var = 5 # Now it will throw an error because it knows my_var is an integer! my_var.reverse For more information on these and more advanced data types, consult the official Python documentation on built-in types.